package com.song.shop.rocketmq;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.selector.SelectMessageQueueByHash;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Song Yinghui
 * @date 2018/8/24
 * @Version 1.0
 */
public class Provide {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {

        DefaultMQProducer product = new DefaultMQProducer("producerGroup");
        product.setNamesrvAddr("192.168.232.137:9876");
        product.setRetryTimesWhenSendFailed(3);
        product.start();
        for (int i = 0 ; i < 10 ; i++){

            Message message = new Message("songtopic" , "TagA" , ("songyinghui 延迟10s消息   "+i).getBytes());
            //message.setDelayTimeLevel(3); //3 代表第三级别 10s延迟（延迟是指消息10s后推送给客户端，不是10s后再发送）
            System.out.println("发送时间---------"+ new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date()));
            SendResult sendResult = product.send(message);
            System.out.println(sendResult);
        }
        //product.send(new Message() , new SelectMessageQueueByHash() , "");
        /*for (int i=0 ; i < 5 ; i++){
            Message message = new Message("songtopic" , "TagA" , ("Hello RocketMQ. This is Queue MQ"+i).getBytes());
            message.setDelayTimeLevel(0);
            SendResult send = product.send(message, (list, message1, o) -> {
                System.out.println("---------");
                System.out.println(new String(message1.getBody()) + "===" + list.size() + "==="+ o.toString() );
                Integer id = Integer.parseInt(o.toString());
                int index = id % list.size();
                return list.get(index);
            }, "1001");
        }*/
        product.shutdown();

    }
}
